home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / main.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  5KB  |  134 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. Main program for 2to3.
  6. '''
  7. import sys
  8. import os
  9. import logging
  10. import shutil
  11. import optparse
  12. from  import refactor
  13.  
  14. class StdoutRefactoringTool(refactor.RefactoringTool):
  15.     '''
  16.     Prints output to stdout.
  17.     '''
  18.     
  19.     def __init__(self, fixers, options, explicit, nobackups):
  20.         self.nobackups = nobackups
  21.         super(StdoutRefactoringTool, self).__init__(fixers, options, explicit)
  22.  
  23.     
  24.     def log_error(self, msg, *args, **kwargs):
  25.         self.errors.append((msg, args, kwargs))
  26.         self.logger.error(msg, *args, **kwargs)
  27.  
  28.     
  29.     def write_file(self, new_text, filename, old_text):
  30.         if not self.nobackups:
  31.             backup = filename + '.bak'
  32.             if os.path.lexists(backup):
  33.                 
  34.                 try:
  35.                     os.remove(backup)
  36.                 except os.error:
  37.                     err = None
  38.                     self.log_message("Can't remove backup %s", backup)
  39.                 except:
  40.                     None<EXCEPTION MATCH>os.error
  41.                 
  42.  
  43.             None<EXCEPTION MATCH>os.error
  44.             
  45.             try:
  46.                 os.rename(filename, backup)
  47.             except os.error:
  48.                 err = None
  49.                 self.log_message("Can't rename %s to %s", filename, backup)
  50.             except:
  51.                 None<EXCEPTION MATCH>os.error
  52.             
  53.  
  54.         None<EXCEPTION MATCH>os.error
  55.         super(StdoutRefactoringTool, self).write_file(new_text, filename, old_text)
  56.         if not self.nobackups:
  57.             shutil.copymode(backup, filename)
  58.         
  59.  
  60.     
  61.     def print_output(self, lines):
  62.         for line in lines:
  63.             print line
  64.         
  65.  
  66.  
  67.  
  68. def main(fixer_pkg, args = None):
  69.     '''Main program.
  70.  
  71.     Args:
  72.         fixer_pkg: the name of a package where the fixers are located.
  73.         args: optional; a list of command line arguments. If omitted,
  74.               sys.argv[1:] is used.
  75.  
  76.     Returns a suggested exit status (0, 1, 2).
  77.     '''
  78.     parser = optparse.OptionParser(usage = '2to3 [options] file|dir ...')
  79.     parser.add_option('-d', '--doctests_only', action = 'store_true', help = 'Fix up doctests only')
  80.     parser.add_option('-f', '--fix', action = 'append', default = [], help = 'Each FIX specifies a transformation; default: all')
  81.     parser.add_option('-x', '--nofix', action = 'append', default = [], help = 'Prevent a fixer from being run.')
  82.     parser.add_option('-l', '--list-fixes', action = 'store_true', help = 'List available transformations (fixes/fix_*.py)')
  83.     parser.add_option('-p', '--print-function', action = 'store_true', help = 'Modify the grammar so that print() is a function')
  84.     parser.add_option('-v', '--verbose', action = 'store_true', help = 'More verbose logging')
  85.     parser.add_option('-w', '--write', action = 'store_true', help = 'Write back modified files')
  86.     parser.add_option('-n', '--nobackups', action = 'store_true', default = False, help = "Don't write backups for modified files.")
  87.     refactor_stdin = False
  88.     (options, args) = parser.parse_args(args)
  89.     if not (options.write) and options.nobackups:
  90.         parser.error("Can't use -n without -w")
  91.     
  92.     if options.list_fixes:
  93.         print 'Available transformations for the -f/--fix option:'
  94.         for fixname in refactor.get_all_fix_names(fixer_pkg):
  95.             print fixname
  96.         
  97.         if not args:
  98.             return 0
  99.     
  100.     if not args:
  101.         print >>sys.stderr, 'At least one file or directory argument required.'
  102.         print >>sys.stderr, 'Use --help to show usage.'
  103.         return 2
  104.     level = None if '-' in args else args if options.verbose else logging.INFO
  105.     logging.basicConfig(format = '%(name)s: %(message)s', level = level)
  106.     rt_opts = {
  107.         'print_function': options.print_function }
  108.     avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
  109.     unwanted_fixes = (set,)((lambda .0: for fix in .0:
  110. fixer_pkg + '.fix_' + fix)(options.nofix))
  111.     explicit = set()
  112.     if options.fix:
  113.         all_present = False
  114.         for fix in options.fix:
  115.             if fix == 'all':
  116.                 all_present = True
  117.                 continue
  118.             explicit.add(fixer_pkg + '.fix_' + fix)
  119.         
  120.         requested = None if all_present else explicit
  121.     else:
  122.         requested = avail_fixes.union(explicit)
  123.     fixer_names = requested.difference(unwanted_fixes)
  124.     rt = StdoutRefactoringTool(sorted(fixer_names), rt_opts, sorted(explicit), options.nobackups)
  125.     if not rt.errors:
  126.         if refactor_stdin:
  127.             rt.refactor_stdin()
  128.         else:
  129.             rt.refactor(args, options.write, options.doctests_only)
  130.         rt.summarize()
  131.     
  132.     return int(bool(rt.errors))
  133.  
  134.